package com.sdp.th.controller;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.utils.LocalDateUtil;
import com.sdp.th.db.th_alarm_count.service.IThAlarmCountService;
import com.sdp.th.db.th_eq_alarm_log.service.IThEqAlarmLogService;
import com.sdp.th.db.th_eq_alarm_smoke_log.service.IThEqAlarmSmokeLogService;
import com.sdp.th.db.th_eq_alarm_trend_log.service.IThEqAlarmTrendLogService;
import com.sdp.th.db.th_eq_state.service.impl.ThEqStateServiceImpl;
import com.sdp.th.db.th_eq_state_log.service.IThEqStateLogService;
import com.sdp.th.entity.ThAlarmCount;
import com.sdp.th.utils.ThConstants;
import com.sdp.th.view.ThEqStateView;

@RestController
@RequestMapping("/ThReportController")
public class ThReportController {
	
	@Autowired
	private IThEqStateLogService iThEqStateLogService;
	

	@Autowired
	private IThAlarmCountService iThAlarmCountService;
	
	@Autowired
	private ThEqStateServiceImpl thEqStateServiceImpl;
	
	@Autowired
	private IThEqAlarmLogService iThEqAlarmLogService;
	
	@Autowired
	private IThEqAlarmTrendLogService iThEqAlarmTrendLogService;
	
	@Autowired
	private IThEqAlarmSmokeLogService iThEqAlarmSmokeLogService;

	/**
	 * 查询一段时间内，某一个配电室的温度变化
	 * @param deviceRoom
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@GetMapping(value="/queryTHByRoom")
	public Result<?> queryTHByRoom(
			@RequestParam(name="deviceRoom",required=true) String deviceRoom,
			@RequestParam(name="startDate",required=true) String startDate,
			@RequestParam(name="endDate",required=true) String endDate) {
		LocalDateTime st = LocalDateUtil.String2LocalDateTime(startDate+" 00:00:00");
		LocalDateTime et = LocalDateUtil.String2LocalDateTime(endDate+" 23:59:59");
		return Result.ok(iThEqStateLogService.queryStateLogByRoomAndDate(deviceRoom,st, et));
	}
	
	
	/**
	 * 查询一段时间内，某一个设备的温湿度变化
	 * @param deviceRoom
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@GetMapping(value="/queryTHByDeviceNo")
	public Result<?> queryTHByDeviceNo(
			@RequestParam(name="deviceNo",required=true) String deviceNo,
			@RequestParam(name="startDate",required=true) String startDate,
			@RequestParam(name="endDate",required=true) String endDate) {
		
		LocalDateTime st = null;
		LocalDateTime et = null;
		
		if(startDate.indexOf(":")>0) {
			st = LocalDateUtil.String2LocalDateTime(startDate);
		}else {
			st = LocalDateUtil.String2LocalDateTime(startDate+" 00:00:00");
		}
		
		if(endDate.indexOf(":")>0) {
			et = LocalDateUtil.String2LocalDateTime(endDate);
		}else {
			et = LocalDateUtil.String2LocalDateTime(endDate+" 23:59:59");
		}
		
		return Result.ok(iThEqStateLogService.queryStateLogByEqAndDate(deviceNo,st, et));
	}
	
	
	/**
	 * 查询设备和所有报警数量
	 * @return
	 */
	@GetMapping(value="/queryEqAndAlarmCount")
	public Result<?> queryEqAndAlarmCount(){
		
		JSONObject res = new JSONObject();
	
		ThAlarmCount thAlarmCount = iThAlarmCountService.queryThAlarmCount();
		
		
		//设备数量
//		List<ThEqInfo> eqList = iThEqInfoService.list();
		
		QueryWrapper<ThEqStateView> queryWrapper = new QueryWrapper<>();
		List<ThEqStateView> eqList = thEqStateServiceImpl.viewList(queryWrapper, null);
		
		BigDecimal onLineCount = BigDecimal.ZERO;
		BigDecimal offLineCount = BigDecimal.ZERO;
		BigDecimal faultCount = BigDecimal.ZERO;
		BigDecimal smokeCount = BigDecimal.ZERO;
		BigDecimal thCount = BigDecimal.ZERO;
		
		for(ThEqStateView state : eqList) {
			if(ThConstants.是.getCode().equals(state.getLineState())) {
				onLineCount = BigDecimal.ONE.add(onLineCount);
			}else {
				offLineCount = offLineCount.add(BigDecimal.ONE);
			}
			
			if(ThConstants.设备状态_故障.getCode().equals(state.getDeviceState())) {
				faultCount = BigDecimal.ONE.add(faultCount);
			}
			
			if(ThConstants.设备类型_温湿度变送器.getCode().equals(state.getDeviceType())) {
				thCount = BigDecimal.ONE.add(thCount);
			}
			
			if(ThConstants.设备类型_烟感探测器.getCode().equals(state.getDeviceType())) {
				smokeCount = BigDecimal.ONE.add(smokeCount);
			}
		}
		
		res.put("totalCount", eqList.size());
		res.put("onLineCount", onLineCount);
		res.put("offLineCount", offLineCount);
		
		res.put("faultCount", faultCount);
		res.put("smokeCount", smokeCount);
		res.put("thCount", thCount);
		
		
		res.put("alarmThCount", thAlarmCount.getAlarmThCount());
		res.put("alarmTrendCount", thAlarmCount.getAlarmTrendCount());
		res.put("alarmSmokeCount", thAlarmCount.getAlarmSmokeCount());
		
		return Result.ok(res);
	}
	
	/**
	 * 查询需要处理的报警数量
	 * @return
	 */
	@GetMapping(value="/queryNeedHandleAlarmCount")
	public Result<?> queryNeedHandleAlarmCount(){
		JSONObject res = new JSONObject();
		res.put("thHandle", 0);
		res.put("trendHandle", 0);
		res.put("smokeHandle", 0);

		//查询待处理的温湿度报警数量
		int thCount = iThEqAlarmLogService.needHandleAlarmCount();
		res.put("thHandle", thCount);
		
		//查询待处理的趋势报警数量
		int trendCount = iThEqAlarmTrendLogService.needHandleAlarmCount();
		res.put("trendHandle", trendCount);
		
		//查询待处理的烟感报警数量
		int smokeHandle  = iThEqAlarmSmokeLogService.needHandleAlarmCount();
		res.put("smokeHandle", smokeHandle);
		
		//查询待处理的烟感报警数量
		return Result.ok(res);
	}
	
}
